import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import smsAppIdSelectUtils from '@/components/sms/smsAppIdSelectUtils'
import smsSupplierTypeSelectUtils from '@/components/sms/smsSupplierTypeSelectUtils'
import smsSendRecordStatusSelectUtils from '@/components/sms/smsSendRecordStatusSelectUtils'
import smsAppIdColumnUtils from '@/components/sms/smsAppIdColumnUtils'
import smsSupplierTypeColumnUtils from '@/components/sms/smsSupplierTypeColumnUtils'
import smsSendRecordStatusColumnUtils from '@/components/sms/smsSendRecordStatusColumnUtils'
import { ActionSchema } from 'amis'
import smsSendFormUtils from '@/components/sms/smsSendFormUtils'
import smsAdminApiConfig from '@/core/sms/generated/apiconfig/smsAdminApiConfig'

const SmsSendRecordList = () => {
	const entityName = '短信发送记录'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = smsAdminApiConfig.smsSendRecord
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'smsAppId',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'identifier',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'phoneNumber',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'supplierType',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'status',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'planSendTime',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: undefined,
		deleteServerApi: undefined,
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
		},
		editServerApi: undefined,
		debug: false,
		toolButtons: [
			{
				type: 'button',
				label: '发送短信',
				actionType: 'drawer',
				level: 'primary',
				drawer: {
					title: '发送短信 (Esc退出)',
					showErrorMsg: false,
					position: 'right',
					closeOnEsc: true,
					resizable: true,
					body: smsSendFormUtils.getSchema(),
				},
			} as ActionSchema,
		],
		formItems: [],
		columns: [
			{
				name: 'id',
				label: 'ID',
				searchable: true,
			},
			{
				...smsAppIdColumnUtils.getSchema(),
				searchable: smsAppIdSelectUtils.getSchema(),
			},
			{
				name: 'identifier',
				label: '编号',
				searchable: true,
			},
			{
				name: 'phoneNumber',
				label: '手机号码',
				searchable: true,
			},
			{
				name: 'content',
				label: '短信内容',
			},
			{
				...smsSupplierTypeColumnUtils.getSchema({ name: 'supplierType' }),
				searchable: smsSupplierTypeSelectUtils.getSchema({ name: 'supplierType' }),
			},
			{
				...smsSendRecordStatusColumnUtils.getSchema(),
				searchable: smsSendRecordStatusSelectUtils.getSchema(),
			},
			{
				name: 'resultRemark',
				label: '发送结果',
			},
			{
				type: 'date',
				name: 'planSendTime',
				label: '计划发送时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'arrivalTime',
				label: '实际发送时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default SmsSendRecordList
